import {
    Body,
    Controller,
    FileTypeValidator,
    Get,
    MaxFileSizeValidator,
    Param,
    ParseFilePipe,
    Patch,
    Post,
    Req,
    UploadedFile,
    UseInterceptors
} from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";
import { ApiBearerAuth, ApiConsumes, ApiOperation, ApiTags } from "@nestjs/swagger";
import { Express, Request } from "express";

import { MIMETypes } from "@/core/constant/global-enum";
import { ApiBaseResponse } from "@/core/decorators/api-base-response.decorator";
import AjaxResult from "@/core/utils/AjaxResult";

import { CreateFileDto } from "../file/dto/create-file.dto";
import { UpdatePasswordDto } from "./dto/update-password.dto";
import { UpdateUserDto } from "./dto/update-user.dto";
import { UserService } from "./user.service";
import { UserVo } from "./vo/user.vo";

@ApiTags("用户")
@ApiBearerAuth()
@Controller("user")
export class UserController {
    constructor(private readonly userService: UserService) {}

    // @ApiOperation({ summary: "创建用户" })
    // @ApiCreatedResponse({ type: BaseSuccessResponseVO<UserVo> })
    // @RequireRoles(AdminSystemRole.SuperAdmin, AdminSystemRole.Admin, AdminSystemRole.Test)
    // @Post()
    // async create(@Body() createUserDto: CreateUserDto): Promise<BaseSuccessResponseVO<UserVo>> {
    //     const res = await this.userService.create(createUserDto);
    //     return AjaxResult.success(res);
    // }

    // @ApiOperation({ summary: "分页查询所有用户" })
    // @ApiResponse({ type: BaseSuccessResponseVO<PageResultVO<UserVo>> })
    // @RequireRoles(AdminSystemRole.SuperAdmin, AdminSystemRole.Admin, AdminSystemRole.Test)
    // @Get()
    // async findListPage(@Query() queryUserDto: QueryUserDto, @Req() req: Request): Promise<BaseSuccessResponseVO<PageResultVO<UserVo>>> {
    //     const res = await this.userService.findListPage(queryUserDto, req);
    //     return AjaxResult.success(res);
    // }

    @ApiOperation({ summary: "用户编辑" })
    @ApiBaseResponse(UserVo)
    @Patch("/edit/:id")
    async update(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto, @Req() req: Request) {
        return AjaxResult.success(await this.userService.update(+id, updateUserDto, req));
    }

    @ApiOperation({ summary: "密码更新" })
    @Patch("/edit/:id/password")
    async updatePassword(@Param("id") id: string, @Body() updatePasswordDto: UpdatePasswordDto) {
        return AjaxResult.success(
            await this.userService.updatePassword(+id, updatePasswordDto.oldPassword, updatePasswordDto.newPassword)
        );
    }

    // @ApiOperation({ summary: "收藏帖子" })
    // @Get("savePost")
    // async savePost(@Query("postId") postId: string, @Req() req: Request) {
    //     return AjaxResult.success(await this.userService.savePost(req.user.id, postId));
    // }

    // @ApiOperation({ summary: "帖子列表", description: "用户详情页显示发布和收藏的帖子" })
    // @Get("profilePosts")
    // async getProfilePosts(@Req() req: Request, @Query() pageDto: PageDto) {
    //     return AjaxResult.success(await this.userService.getProfilePosts(req.user.id, pageDto));
    // }

    // @ApiOperation({ summary: "消息通知数目" })
    // @Get("notifyCount")
    // async getNotifyCount(@Req() req: Request) {
    //     return AjaxResult.success(await this.userService.getNotifyCount(req.user.id));
    // }

    @ApiOperation({ summary: "用户自身详情" })
    @ApiBaseResponse(UserVo)
    @Get("/self")
    async findSelfInfo(@Req() req: Request) {
        return AjaxResult.success(await this.userService.findOne(req.user.id, req));
    }

    @ApiOperation({ summary: "用户详情" })
    @ApiBaseResponse(UserVo)
    @Get("/detail/:id")
    async findOne(@Param("id") id: string, @Req() req: Request) {
        return AjaxResult.success(await this.userService.findOne(+id, req));
    }

    @ApiOperation({ summary: "头像上传" })
    @ApiConsumes("multipart/form-data")
    @Post("/avatar")
    @UseInterceptors(FileInterceptor("file", { limits: { files: 1 } }))
    async uploadAvatar(
        @UploadedFile(
            new ParseFilePipe({
                validators: [
                    new MaxFileSizeValidator({ maxSize: 1024 * 1024 * 5 }),
                    new FileTypeValidator({ fileType: MIMETypes.ALL_IMAGE })
                ]
            })
        )
        file: Express.Multer.File,
        @Body() createFileDto: CreateFileDto,
        @Req() req: Request
    ) {
        return AjaxResult.success(await this.userService.uploadAvatar(req, createFileDto, file));
    }
}
